package sy.service.impl;

import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Service;
import sy.pageModel.BaseEntity;
import sy.pageModel.DataGrid;
import sy.service.MassageServiceI;
import sy.util.StringUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016-07-21.
 */
@Service
public class MassageServiceImpl extends BaseService implements MassageServiceI{

    @Override
    public DataGrid findMassageList(BaseEntity curr) throws Exception {
        DataGrid dg = new DataGrid();
        String sql = "SELECT count(1) from t_massage tm,t_massage_user utm,tuser t where tm.id=utm.massage_id and t.id=utm.user_id ";
        sql = addWhere(curr, sql);
        long total = template.queryForLong(sql, new Object[]{});
        dg.setTotal(total);
        sql = "SELECT *from t_massage tm,t_massage_user utm,tuser t where tm.id=utm.massage_id and t.id=utm.user_id  ";
        sql = addWhere(curr, sql);
        sql = addOrderBy(curr, sql);
        sql = addLimit(curr, sql);
        final List<BaseEntity> currList = new ArrayList<BaseEntity>();
        template.query(sql, new Object[]{}, new RowCallbackHandler(){
            public void processRow(ResultSet rs) throws SQLException {
                BaseEntity entity = new BaseEntity();
                entity.setId(rs.getString("utm.massage_id")+"~"+rs.getString("utm.user_id"));
                entity.setName(rs.getString("t.name"));
                entity.setTypeName(rs.getString("tm.name"));
                entity.setText(rs.getString("t.email"));
                currList.add(entity);
            }});
        dg.setRows(currList);
        return dg;
    }

    @Override
    public BaseEntity editBaseEntity(BaseEntity curr) throws Exception {
        return null;
    }

    @Override
    public BaseEntity addBaseEntity(BaseEntity curr) throws Exception {
        String sql="insert into t_massage_user (massage_id, user_id) values (?,?);";
        template.update(sql,new Object[]{curr.getTypeId(),curr.getId()});
        return curr;
    }

    @Override
    public BaseEntity deleteBaseEntity(BaseEntity curr) throws Exception {
        String sql="DELETE FROM t_massage_user where massage_id=? and user_id=?";
        String ids1[]=curr.getIds().split(",");
        for (int i = 0; i <ids1.length ; i++) {
            String str[]=ids1[i].split("~");
            template.update(sql,new Object[]{str[0],str[1]});
        }
        return curr;
    }

    @Override
    public List<BaseEntity> findTuser(String table) {
        String sql="select * from "+table;
        final List<BaseEntity> entityList=new ArrayList<>();
        template.query(sql, new Object[]{}, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                BaseEntity entity=new BaseEntity();
                entity.setId(rs.getString("id"));
                entity.setText(rs.getString("name"));
                entityList.add(entity);
            }
        });
        return entityList;
    }


    private String addWhere(BaseEntity curr, String sql) {
        if(!StringUtil.isNullOrEmpty(curr.getName())) {
            sql += " and t.name like '%" + curr.getName() + "%'";
        }
        if(!StringUtil.isNullOrEmpty(curr.getText())) {
            sql += " and t.email like '%" + curr.getText() + "%'";
        }
        return sql;
    }

    private String addLimit(BaseEntity curr, String sql) {
        int fromRow = (curr.getPage() - 1) * curr.getRows();
        sql += " limit " + fromRow + "," + curr.getRows();
        return sql;
    }

    private String addOrderBy(BaseEntity curr, String sql) {
        if (curr.getSort() != null) {
            if("name".equals(curr.getSort())) {
                curr.setSort("t.name");
            }
            if("text".equals(curr.getSort())) {
                curr.setSort("t.email");
            }
            if("typeName".equals(curr.getSort())) {
                curr.setSort("tm.name");
            }
            if("id".equals(curr.getSort())) {
                curr.setSort("t.id");
            }
            sql += " order by " + curr.getSort() + " " + curr.getOrder();
        }
        return sql;
    }

}
